package features.advance.jdk.stream;

import java.io.*;
import java.util.Date;
import java.util.function.Function;

/**
 * @author LIN
 * @date 2022-05-06 17:09
 *
 * 对象的序列化和反序列化
 *
 */
public class ObjectSerialize {

    public static void main(String[] args) {
        String fileName = "t.tmp";

        try {
            FileOutputStream fos = new FileOutputStream(fileName);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeInt(12345);
            oos.writeObject("Today");
            oos.writeObject(new Date());
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        File file = new File(fileName);
        if(file.exists()){
            try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))){
                int a = ois.readInt();
                System.out.println(a);
                Object readObject = ois.readObject();
                System.out.println(readObject);
                Object o = ois.readObject();
                System.out.println(o);

            }catch (Exception e){
                e.printStackTrace();
            }finally {

            }
        }



        Function f1 = new Function() {
            @Override
            public Object apply(Object o) {
                System.out.println(o);
                return null;
            }
        };
        execute(f1,f1,f1);
    }

    public static void execute(Function pre, Function doExe, Function after){
        doExe.apply(null);
    }


}
